package com.swings.application.utility;

import java.io.Serializable;

import org.apache.log4j.Logger;

import com.swings.base.UtilityBaseFrame;

/**
 * <p>
 * <b>Overview:</b>
 * <p>
 * 
 * 
 * <pre>
 * Creation date: Feb 16, 2015
 * @author Amit Kshirsagar
 * @version 1.0
 * @since
 * 
 * <p><b>Modification History:</b><p>
 * 
 * 
 * </pre>
 */

public class CaseSignalProcessor implements Runnable, Serializable {
	static Logger log = Logger.getLogger(CaseSignalProcessor.class.getName());

	public volatile boolean continueRun = false;
	private String phraseName = null;
	private int caseCount = -1;

	private UtilityBaseFrame mainFrame = null;
	private boolean isStarted = false;
	private boolean isRunning = false;

	/**
	 * @return the isRunning
	 */
	public boolean isRunning() {
		return isRunning;
	}

	/**
	 * @param isRunning
	 *            the isRunning to set
	 */
	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

	/**
	 * @return the isStarted
	 */
	public boolean isStarted() {
		return isStarted;
	}

	/**
	 * @param isStarted
	 *            the isStarted to set
	 */
	public void setStarted(boolean isStarted) {
		this.isStarted = isStarted;
	}

	/**
	 * @return the caseCount
	 */
	public int getCaseCount() {
		return caseCount;
	}

	/**
	 * @param caseCount
	 *            the caseCount to set
	 */
	public void setCaseCount(int caseCount) {
		this.caseCount = caseCount;
	}

	public CaseSignalProcessor(String phraseName, String caseCount, UtilityBaseFrame mainFrame) {
		this.phraseName = phraseName;
		this.caseCount = Integer.parseInt(caseCount);
		this.mainFrame = mainFrame;
		modnumber = (this.caseCount / 100) + 1;
		continueRun = true;
	}

	/**
	 * @return the phraseName
	 */
	public String getPhraseName() {
		return phraseName;
	}

	/**
	 * @param phraseName
	 *            the phraseName to set
	 */
	public void setPhraseName(String phraseName) {
		this.phraseName = phraseName;
	}

	public void pauseThread() throws InterruptedException {
		continueRun = false;
		setRunning(false);
	}

	public void resumeThread() {
		continueRun = true;
		setRunning(true);
	}

	int modnumber = (caseCount / 100) + 1;
	int processedCount = 0;
	int progressCount = 0;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public synchronized void run() {
		setStarted(true);
		setRunning(true);
		System.out.println(modnumber);
		while (processedCount < caseCount) {
			while (!continueRun) {
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			processedCount = processedCount + modnumber;
			if (processedCount > caseCount) {
				processedCount = caseCount;
			}
			progressCount = Math.round(processedCount * 100 / caseCount);
			mainFrame.setProgressStatus(progressCount, phraseName + " Processed : " + processedCount + "/" + caseCount
					+ " : " + progressCount + "%");
			sleep();
		}

	}

	/**
	 * 
	 */
	private void sleep() {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 
	 */
	public void updateProgressStatus() {
		mainFrame.setProgressStatus(progressCount, phraseName + " Processed : " + processedCount + "/" + caseCount
				+ " : " + progressCount + "%");
	}

	public String getCaseDetailList() {
		return processedCount+"";
	}
}
